package com.inspur_video.inspur_video.controller;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.inspur_video.inspur_video.bean.Response;
import com.inspur_video.inspur_video.bean.tofront.MainPageInfo;
import com.inspur_video.inspur_video.bean.tofront.SearchInfo;
import com.inspur_video.inspur_video.bean.video.Video;
import com.inspur_video.inspur_video.bean.video.VideoList;
import com.inspur_video.inspur_video.bean.video.VideoPlay;
import com.inspur_video.inspur_video.bean.video.VideoUpload;
import com.inspur_video.inspur_video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-07-30 10:34
 * @package com.inspur_video.inspur_video.controller
 * @description 视频获取控制器
 */

@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    HttpSession httpSession;
    @Autowired
    VideoService videoService;

    /**
     * 用户获取
     * 从阿里云（后台）获取视频全部列表
     *
     * @return
     */
    @RequestMapping("/list")
    public VideoList getAllVideo() {
        return videoService.getAllVideo("Normal");
    }

    /**
     * 首页封面列表数据获取
     * select 0 全部  1视频  2电视剧
     *
     * @return
     */
    @RequestMapping("/main_page/{select}")
    public List<List<MainPageInfo.pageInfoItem>> getMainPageInfo(@PathVariable("select") int select) {

        if (select == 0) {
            return videoService.getMainPageInfo();
        } else if (select == 1) {
            return videoService.getMainPageInfoSelect(0);
        } else if (select == 2) {
            return videoService.getMainPageInfoSelect(1);
        }
        return null;
    }

    /**
     * 根据数据库中的视频id查询单个视频信息(带URL)
     * 这里不该用数据库中ID ， 应该用阿里云的ID，这样的前端的视频信息就能统一成阿里云ID，不用阿里云ID和数据库都存着了
     */
    @RequestMapping("/get_info/{id}")
    public Video getOneByVId(@PathVariable("id") String id) {
        return videoService.getVideoInfoByAId(id);
    }


    /**
     * 根据阿里云id查询单个视频信息(无URL),一般直接数据库查询，估计不用阿里云查询
     */
    @RequestMapping("/get_info_ali/{id}")
    public VideoPlay getOneByAId(@PathVariable("id") String id) {
        return videoService.getALiVideoInfo(id);
    }

    /***
     * 获取播放地址（URL）
     */
    @RequestMapping("/get_video_play/{aId}/{definition}")
    public List<GetPlayInfoResponse.PlayInfo> getVideoPlay(@PathVariable("aId") String aId,
                                                           @PathVariable(name = "definition", required = false) String definition) {

        int uId;
        if (httpSession.getAttribute("user") != null) {
            uId = Integer.parseInt(httpSession.getAttribute("user").toString());
        } else {
            uId = 0;
        }
        return videoService.getALiVideoPlay(definition, aId, uId);
    }

    /***
     * 获取播放地址（URL）
     */
    @RequestMapping("/get_video_play/{uid}/{aId}/{definition}")
    public List<GetPlayInfoResponse.PlayInfo> getUserVideoPlay(@PathVariable("uid") int uid,
                                                               @PathVariable("aId") String aId,
                                                           @PathVariable(name = "definition", required = false) String definition) {

//        int uId;
//        if (httpSession.getAttribute("user") != null) {
//            uId = Integer.parseInt(httpSession.getAttribute("user").toString());
//        } else {
//            uId = 0;
//        }
        return videoService.getALiVideoPlay(definition, aId, uid);
    }


    /**
     * 视频上传
     *
     * @param title
     * @param description
     * @param fileName
     * @param tags
     * @param imageExt
     * @param category
     * @param permission
     * @return
     */
    @RequestMapping("/video_upload")
    public VideoUpload videoUpload(String title, String description, String fileName, String tags,
                                   @RequestParam(required = false, defaultValue = "1") Integer number,
                                   @RequestParam(required = false, defaultValue = "1") Integer total,
                                   String imageExt, int category, int permission) {
        int uId = Integer.parseInt(httpSession.getAttribute("user").toString());
        System.out.println(uId);
        return videoService.videoUpload(uId, title, description, fileName, tags, number, total,
                imageExt, category, permission);
    }

    /**
     * 模糊查询（包括三个字段的模糊查询）
     *
     * @param title
     * @param describe
     * @param tags
     * @return
     */
    public List<Video> findByTitleAndDescribeLike(String title, String describe, String tags) {
        if (title == null) {
            title = "";
        }
        if (describe == null) {
            describe = "";
        }
        if (tags == null) {
            tags = "";
        }
        return videoService.findByTitleAndDescribeLike(title, describe, tags);
    }


    /**
     * @param title    用户在前端输入的检索条件，默认就是视频的标题
     * @param classify 1表明查询电影，2表明查询电视剧 ， 默认就是查所有得了
     * @return
     */
    @RequestMapping("/find_like")
    public List<SearchInfo> findByKeyword(String title, @RequestParam(required = false, defaultValue = "0") int classify) {
        List<SearchInfo> searchInfos = new LinkedList<>();
        if (classify == 0) {
            List<Video> videoListByTitle = videoService.findByTitle(title);
            for (Video video : videoListByTitle) {
                SearchInfo searchInfoItem = new SearchInfo();
                searchInfoItem.setTitle(video.getVTitle());
                searchInfoItem.setAId(video.getAId());
                searchInfos.add(searchInfoItem);
            }
            return searchInfos;
        } else if (classify == 1) {
            List<Video> videos = videoService.findByTitleCategory(title, 0);
            for (Video video : videos) {
                SearchInfo searchInfoItem = new SearchInfo();
                searchInfoItem.setTitle(video.getVTitle());
                searchInfoItem.setAId(video.getAId());
                searchInfos.add(searchInfoItem);
            }
            return searchInfos;
        } else if (classify == 2) {
            List<Video> videos = videoService.findByTitleCategory(title, 1);
            for (Video video : videos) {
                SearchInfo searchInfoItem = new SearchInfo();
                searchInfoItem.setTitle(video.getVTitle());
                searchInfoItem.setAId(video.getAId());
                searchInfos.add(searchInfoItem);
            }
            return searchInfos;
        }
        return null;
    }


    /**
     * 删除视频(删除多个，请求参数list集合)
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public Response deleteVideos(@RequestParam(value = "ids[]") List<String> ids) {
        System.out.println(ids);
        boolean b = videoService.deleteVideos(ids);
        if (b) {
            return new Response(200, "success");
        } else {
            return new Response(500, "fail");
        }
    }

    /**
     * 修改视频（单个修改
     *
     * @param aid         阿里云主键
     * @param title       标题
     * @param description 描述
     * @param tags        标签
     * @param permission  会员
     * @param number      当前集数
     * @param total       总集数
     * @return
     */
    @RequestMapping("/update/{id}")
    public Response updateVideos(@PathVariable("id") String aid, String title,
                                 String description, String tags, Integer category,
                                 Integer permission, @RequestParam(required = false, defaultValue = "0") Integer number,
                                 @RequestParam(required = false, defaultValue = "0") Integer total) {
        System.out.println(aid);
        System.out.println(title);
        System.out.println(description);
        System.out.println(category);
        System.out.println(permission);
        System.out.println(number);
        System.out.println(total);
        boolean b = videoService.updateVideos(aid, title, description, tags, category, permission, number, total);
        if (b) {
            return new Response(200, "success");
        } else {
            return new Response(500, "fail");
        }
    }

    /**
     * 所有影片分页返回
     *
     * @param num
     * @param size
     * @return
     */
    @RequestMapping("/listPage")
    public Page<Video> getList(@RequestParam(required = false, defaultValue = "0") Integer num,
                               @RequestParam(required = false, defaultValue = "12") Integer size) {
        Page<Video> videoPage = videoService.getVideoPage(num, size);
        return videoPage;
    }

    /**
     * 根据标题和集数查询电视剧
     *
     * @param title
     * @param number
     * @return
     */
    @RequestMapping("/find_tv")
    public List<Video> getTVByTitle(@RequestParam(defaultValue = "") String title, int number) {

        return videoService.getTVByTitle(title, number);

    }


}
